/**
 * 获取全局App
 */
const app = getApp()
 
/**
 * 请求函数
 * @param {*} path 
 * @param {*} method 
 * @param {*} data 
 */
const requestData = (path, method, data) => {
  return new Promise(function(resolve, reject) {
    // let token = app.globalData.token
    // if (token == null) {
    //   token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
    // }
    wx.request({
      url: app.globalData.url + path,
      data: data,
      method: method,
      header: {
        'content-type': 'application/json',
        // 'token': token
      },
      success: function(res) {
        if (res.statusCode == 200) {
          resolve(res.data)
        } else {
          reject(e)
        }
      },
      fail: function(e) {
        //e = { errMsg: "request:fail invalid url" }
        e.errMsg = "网络请求失败"
        reject(e)
      }
    })
  })
}
/**
 * get请求
 * @param {*} path 
 * @param {*} data 
 */
function getData(path, data) {
  return requestData(path, 'GET', data)
}
 
/**
 * post请求
 * @param {*} path 
 * @param {*} data 
 */
function postData(path, data) {
  return requestData(path, 'POST', data)
}
 
/**
 * 导出get请求与post请求函数
 */ 
module.exports = {
  GET: getData,
  POST: postData
}